%{
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define DATE_FORMAT ("%Y/%B/%d\t%T  (%Z %z)")
#define ARRAY_SZ (256)

char* datestring = NULL;

%}

%debug

%union {char* s; int i;}

%token <s> COLOR 
%token <s> XCOLOR
%token <i> INT
%token THE_END
%token DEFINE
%token IFNDEF
%token ENDIF

%type <s> definitions
%%

input: IFNDEF DEFINE definitions {
	FILE *out = fopen("Color.java", "w");
	fprintf(out, 
"/**\n\
* Automatically generated on %s\n\
**/\n\
package termint.game.monsters;\n\
public enum Color {\n\
\t%s\n\
\tprivate int val;\n\
\tprivate Color(int v) {\n\
\t\tthis.val = v;\n\
\t}\n\
\tprivate Color(Color c) {\n\
\t\tthis.val = c.val;\n\
\t}\n\
}\n", datestring, $3);
	fclose(out);
	YYACCEPT;
};

definitions: DEFINE COLOR INT definitions { asprintf(&$$, "%s(%d), %s", $2, $3, $4); }
| DEFINE XCOLOR COLOR definitions         { asprintf(&$$, "%s(%s), %s", $2, $3, $4); }
| DEFINE XCOLOR COLOR ENDIF 			  { asprintf(&$$, "%s(%s);", $2, $3); }
;

%%
extern int yylineno;
extern char* yytext;
int yyerror(char *s, int lineNumber) {
  fprintf(stderr, "Error: %s at %d: '%s'\n", s, yylineno, yytext);
  return 0;
}

int main(int argc, char** argv) {
	time_t curtime = time((time_t*)0);
	datestring = calloc(ARRAY_SZ, sizeof(char));
	strftime(datestring, ARRAY_SZ, DATE_FORMAT, localtime(&curtime));

	FILE* in_file = fopen("nhsrc/color.h", "r");
	yyrestart(in_file);
	yyparse();
	fclose(in_file);
	return 0;
}
